package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxxx.server.mapper.MailLogMapper;
import com.xxxx.server.pojo.*;
import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2022-04-13
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MailLogMapper mailLogMapper;
    @Override
    public RespPageBean employeepage(Integer currentPage, Integer size, Employee employee, LocalDate[] beginDateScope) {
        Page page=new Page<Employee>(currentPage,size);
       IPage<Employee> iPage=employeeMapper.employeepage(page,employee,beginDateScope);
       return new RespPageBean(iPage.getTotal(),iPage.getRecords());
    }
    @Override
    public RespBean getmaxid(){
       List<Map<String,Object>> workID=employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(workID)"));
      String workID2=(String) workID.get(0).get("max(workID)");
      if (null!=workID2&&""!=workID2) {
          return RespBean.success(null,String.format("%08d", Integer.parseInt(workID2) + 1));
      }else {
          return RespBean.error("查询失败");
      }
    }
    @Override
    public RespBean addemployee(Employee employee) {
        /*1取时间转换
        * 2转换时间为年率
        * 3插入
        * 4处理发送内容记录
        * 5发送一条入职欢迎到rabbitmq
        * */
        LocalDate beginContract=employee.getBeginContract();
        LocalDate endContract = employee.getEndContract();
        long day=beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat =new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(day/365.00)));
        if (1==employeeMapper.insert(employee)){
            Employee emp=employeeMapper.getemployee(employee.getId()).get(0);
           String msgId= UUID.randomUUID().toString();
            MailLog mailLog =new MailLog();
            mailLog.setMsgId(msgId);
            mailLog.setEid(employee.getId());
            mailLog.setStatus(0);
            mailLog.setRouteKey(MailConstants.MAIL_ROUTER_KEY_NAME);
            mailLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
            mailLog.setCount(MailConstants.MAX_TRY_COUNT);
            mailLog.setCreateTime(LocalDateTime.now());
            mailLog.setTryTime(LocalDateTime.now().plusMinutes(MailConstants.MSG_TIMEOUT));
            mailLog.setUpdateTime(LocalDateTime.now());
            mailLogMapper.insert(mailLog);
            //发送消息
            rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME,MailConstants.MAIL_ROUTER_KEY_NAME,emp,
                    new CorrelationData(msgId));
            return RespBean.success("添加成功");
        }else{
            return RespBean.error("添加失败");
        }
    }
    @Override
    public List<Employee> getemployee(Object o) {
        return employeeMapper.getemployee(o);
    }
    @Override
    public RespPageBean getEmployeeWithSalary(Integer currentPage, Integer size) {
       IPage<Employee> employeePage= new Page<>(currentPage,size);
        IPage iPage =employeeMapper.getEmployeeWithSalary(employeePage);
       return new RespPageBean(iPage.getTotal(),iPage.getRecords());
    }
}
